import cn.sh.yhk.Application;
import cn.sh.yhk.configure.elasticsearch.EsRestService;
import cn.sh.yhk.mapper.basic.SUserMapper;
import cn.sh.yhk.model.basic.SUser;
import cn.sh.yhk.model.basic.SUserExample;
import com.alibaba.fastjson.JSONObject;
import org.frameworkset.elasticsearch.ElasticSearchHelper;
import org.frameworkset.elasticsearch.boot.BBossESStarter;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.frameworkset.elasticsearch.entity.ESDatas;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: adminyhk
 * @Date: 2018/9/30 09:57
 * @Description:
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class ES {

//    @Resource
//    EsRestService esRestService;
    @Resource
    BBossESStarter bbossESStarter;

    @Resource
    SUserMapper sUserMapper;

    @Test
    public void es1() throws Exception {
//        SUserExample sUserExample = new SUserExample();
//        sUserExample.createCriteria().andUserIdEqualTo(1L);
//        SUser user = sUserMapper.selectByExample(sUserExample).get(0);
//        user.setUserId(null);
//        System.out.println("======================================");
//        for (int i = 1; i <= 1000000; i++) {
//            user.setUserName(i+"");
//            sUserMapper.insert(user);
//        }
//        System.out.println("======================================");
        //dsl
        ClientInterface clientUtil = bbossESStarter.getConfigRestClient("esmapper/demo.xml");
        Map<String, Object> params = new HashMap<>();
        //设置applicationName1和applicationName2两个变量的值
        params.put("od", "SHA-LGA");
        params.put("rt", "ODOW");
        params.put("beginDate", "2019-12-01T00:00:00.000z");
        params.put("endDate", "2019-12-26T17:00:00.000z");
        params.put("cardinality", "id.keyword");
        ESDatas<Map> esDatas =  //ESDatas包含当前检索的记录集合，最多1000条记录，由dsl中的size属性指定
                clientUtil.searchList("oddate*/_search",//demo为索引表，_search为检索操作action
                        "testHighlightSearch",//esmapper/demo.xml中定义的dsl语句
                        params,//变量参数
                        Map.class);//返回的文档封装对象类型
        System.out.println(JSONObject.toJSONString(esDatas));
        //sql转dsl
//        ClientInterface clientUtil1 = ElasticSearchHelper.getRestClientUtil();
//        String json = clientUtil1.executeHttp("/_xpack/sql/translate",
//                "{\n" +
//                        "  \"query\": \"select method,count(method) FROM rpcfilter201907 group by method\"\n" +
//                        "}",
//                ClientInterface.HTTP_POST
//        );
//        System.out.println(json);

        ClientInterface clientUtil1 = ElasticSearchHelper.getRestClientUtil();
        String json = clientUtil1.executeHttp("/_xpack/sql?format=json",
                "{\n" +
                        " \"query\": \"select count(1)  FROM oddate201910\"\n" +
                        "}",
                ClientInterface.HTTP_POST
        );
        System.out.println(json);


//        for (ESIndice esIndice : esRestService.restClient().getIndexes()) {
//            System.out.println(esIndice.getIndex() + "=" + esIndice.getStatus());
//        }
//        Map<String, Object> params = new HashMap<>();
//        params.put("url", "/error");
//        esRestService.restClient().addDocument("abc", "abc", params);
        //6.2
//        List<ESIndice> list = esRestService.restClient().getIndexes();
//        for (ESIndice esIndice : list) {
//            System.out.println(esIndice.getIndex() + "=" + esIndice.getStatus());
//        }
//        ESDatas esDatas = esRestService.restClient().searchAll("test-2018.12.11", JSONObject.class);
//        for (Object obj : esDatas.getDatas()) {
//            System.out.println(obj);
//        }
//        Map<String, Object> params = new HashMap<>();
//        params.put("url", "/error");
//        String json = esRestService.restClient().executeHttp("/_xpack/sql?format=txt",
//                "{\"query\": \"SELECT * FROM test-2018.12.11\"}",
//                ClientInterface.HTTP_POST);
//        System.out.println(json);
        //6.2
//        RestHighLevelClient client = new RestHighLevelClient(
//                RestClient.builder(
//                        new HttpHost("localhost", 9200, "http")
//                ));
//        System.out.println(client.ping());
        //读取SOURCE
//        GetRequest getRequest = new GetRequest(
//                "nvitebids2");//文档ID
//        getRequest.id("nUcbhmcBQ2pPqPPhz9AP");
//        GetResponse getResponse1 = client.get(getRequest);
//        System.out.println(getResponse1.getType());
//        System.out.println(getResponse1.isExists());
//        System.out.println(JSONObject.toJSON(getResponse1.getSource()));
        //存source
//        String json = "{\"flag\":\"b2b_f\",\"redirectUrl\":\"http://localhost:8088/home_f.do\",\"url\":\"/ceair_sso/login.html\"}";
////        InviteBidsExample inviteBidsExample = new InviteBidsExample();
////        List<InviteBids> list = inviteBidsMapper.selectByExample(inviteBidsExample);
//        Long x = System.currentTimeMillis();
//        IndexRequest indexRequest2 = new IndexRequest("nvitebids3", "bids2");
//        indexRequest2.source(json, XContentType.JSON);
//        IndexResponse indexResponse = client.index(indexRequest2);
//        System.out.println(x - System.currentTimeMillis());

    }


    //  @Test
    public void jdbc() throws MessagingException {
        //System.out.println(JSONObject.toJSON(jdbcMapper.selectUser()));

    }

    // @Test
    public void mail() throws MessagingException {
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setFrom(fromMail);
//        message.setTo("652264868@qq.com");
//        message.setSubject("测试邮件主题");
//        message.setText("测试邮件内容");
//        javaMailSender.send(message);
    }

    // @Test
    public void es() throws MessagingException, IOException {
//        IndexRequest request = new IndexRequest("buydeem", "order", "1");
//        Map<String, Object> order = new HashMap<>();
//        order.put("orderNo", "201807240001");
//        order.put("created", new Date());
//        order.put("amount", BigDecimal.TEN);
//        request.source(order);
//        IndexResponse response = client.index(request);
        //   System.out.println(response.toString());
    }
}
